package com.nowcoder.controller;

import com.nowcoder.model.HostHolder;
import com.nowcoder.model.Message;
import com.nowcoder.model.User;
import com.nowcoder.model.ViewObject;
import com.nowcoder.service.MessageService;
import com.nowcoder.service.UserService;
import com.nowcoder.util.WendaUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class MessageController {

    private static final Logger logger = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    private MessageService messageService;

    @Autowired
    HostHolder hostHolder;

    @Autowired
    UserService userService;

    @RequestMapping(value = {"/msg/addMessage"},method = {RequestMethod.POST})
    @ResponseBody
    public String addMessage(@RequestParam("toName")String toName,
                             @RequestParam("content")String content) {
        try{
            //如果用户未登录,则返回相应信息
            if(hostHolder.getUser() == null){
                return WendaUtil.getJSONString(999,"未登录");
            }

            //获取要发送给私信的用户
            User user = userService.selectByName(toName);
            if(user == null){
                return WendaUtil.getJSONString(1,"用户不存在");
            }

            Message message = new Message();
            message.setCreatedDate(new Date());
            message.setFromId(hostHolder.getUser().getId());
            message.setToId(user.getId());
            message.setContent(content);
            messageService.addMessage(message);

            //返回0表示成功
            return WendaUtil.getJSONString(0);
        }catch (Exception e){
            logger.error("发送消息失败" + e.getMessage());
            return WendaUtil.getJSONString(1,"发信失败");
        }
    }

    //获取已一个用户的所有私信列表(私信和发送私信的用户)
    @RequestMapping(value = {"/msg/list"},method = {RequestMethod.GET})
    public String getConversationList(Model model) {
        if(hostHolder.getUser() == null){
            return "redirect:/reglogin";
        }
        int localUserId = hostHolder.getUser().getId();
        List<Message> conversationList = messageService.getConversationList(localUserId,0,10);
        List<ViewObject> conversations = new ArrayList<>();
        for(Message message : conversationList){
            ViewObject vo = new ViewObject();
            vo.set("conversation",message);
            int targetId = message.getFromId() == localUserId ? message.getToId() : message.getFromId();
            vo.set("user",userService.getUser(targetId));
            vo.set("unread",messageService.getConvesationUnreadCount(localUserId,message.getConversationId()));
            conversations.add(vo);
        }
        model.addAttribute("conversations",conversations);//将多条会话信息添加到Model中
        return "letter";
    }

    //将所有Message详情获取出来
    //就是将一个对话的Message取出来
    @RequestMapping(value = {"/msg/detail"},method = {RequestMethod.GET})
    public String getConversationDetail(Model model, @RequestParam("conversationId")String conversationId) {
        try{
            List<Message> messageList = messageService.getConversationDetail(conversationId,0,10);//取出10条私信
            //获取VO对象
            List<ViewObject> messages = new ArrayList<>();
            for(Message message : messageList){
                //这里取出来的Message对象中的created_date不是标准格式的,在html页面要将
                //它转换为标准的时间显示出来
                System.out.println(message.getCreatedDate());
                ViewObject vo = new ViewObject();
                vo.set("message",message);
                vo.set("user",userService.getUser(message.getFromId()));
                messages.add(vo);
            }
            model.addAttribute("messages",messages);
        }catch (Exception e){
            logger.error("获取详情失败" + e.getMessage());
        }
        return "letterDetail";
    }



}
